<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 左侧 -->
        <el-row type="flex" justify="space-between">
          <el-col>
            <span>
              <el-form :inline="true" :model="list" class="demo-form-inline">
                <el-form-item>
                  <el-input v-model="list.subject_name" size="small" placeholder="学科名称" />
                </el-form-item>
                <el-form-item>
                  <el-select v-model="list.is_show" clearable size="small" placeholder="显示状态">
                    <el-option label="显示" :value="1" />
                    <el-option label="隐藏" :value="2" />
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" size="small" plain @click="onSubmit">搜索</el-button>
                  <el-button type="primary" size="small" plain @click="reset">重置</el-button>
                </el-form-item>
              </el-form>
            </span>
          </el-col>
          <el-col :span="6">
            <el-button style="float: right" type="primary" plain @click="showTanchaung">创建学科</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格 -->
      <div class="text item">
        <el-table
          :data="tableData"
          :border="true"

          style="width: 100%"
        >
          <el-table-column
            type="index"
            label="序号"
            width="120"
            align="center"
          />
          <el-table-column
            prop="id"
            label="学科ID"
            width="70"
            align="center"
          />
          <el-table-column
            prop="subject_name"
            label="学科名称"
          />
          <el-table-column
            prop="subject_icon"
            label="学科图标"
            align="center"
          >
            <template slot-scope="{row}">
              <img v-if="row.subject_icon" :src="showimg(row.subject_icon)" height="40" alt="">
              <div v-else>暂无</div>
            </template>

          </el-table-column>
          <el-table-column
            prop="username"
            label="创建人"
            align="center"
          />
          <el-table-column
            prop="is_show"
            label="是否显示"
            align="center"
          >
            <template slot-scope="{row}">
              <el-switch
                v-model="row.is_show"
                active-text="是"
                inactive-text="否"
                :active-value="1"
                :inactive-value="2"
                @change="swithChange(row.id,row.is_show)"
              />
            </template>
          </el-table-column>
          <el-table-column
            prop="cate_num"
            label="目录数量"
            width="150"
            align="center"
          >
            <template slot-scope="{row}">
              <el-link type="primary" @click="tiaozhaun(row)">{{ row.cate_num }}<i class="el-icon-paperclip" /></el-link>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="160"
            align="center"
          >
            <template slot-scope="{row}">
              <el-button type="primary" icon="el-icon-edit" circle @click="handleClick(row.id)" />
              <el-button type="danger" icon="el-icon-delete" circle @click="delHandle(row.id,row.subject_name)" />
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <br>
      <el-pagination
        background
        :current-page="list.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="list.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </el-card>
    <addSubject ref="formdata" @updateList="getInfo" />
  </div>
</template>

<script>
import { getXuekeInfo, changeSwith, delInfo, updataXuekeInfo, goXuekeInfo } from '@/api/subject'
import { getXuekeInfo1 } from '@/api/directory'
import addSubject from './components/addsubject.vue'
export default {
  name: 'Subject',
  components: {
    addSubject
  },
  data() {
    return {
      list: {
        subject_id: '',
        subject_name: '',
        page: 1,
        is_show: '',
        limit: 10
      },
      tableData: [],
      total: 0

      // dangeInfo: {}
    }
  },
  created() {
    this.getInfo()
  },
  methods: {
    // 跳转
    async tiaozhaun(row) {
      this.list.subject_id = row.id
      await goXuekeInfo(this.list)
      console.log(this.list.subject_id)
      if (row.cate_num === 0) return
      // const url = `/subject/directory/?subject_id=${this.list.subject_id}`
      this.$router.push({
        name: 'directory',
        query: {
          subject_id: this.list.subject_id
        }
      })
    },
    // 编辑
    async handleClick(id) {
      const res = await updataXuekeInfo(id)
      console.log(res)
      this.$refs.formdata.form = res
      if (res.subject_icon) {
        const icon = res.subject_icon.split(',')
        console.log(icon)
        this.$refs.formdata.fileList.push({
          url: process.env.VUE_APP_BASE_API + '/' + icon,
          name: this.$refs.formdata.tuName
        })
      }

      this.$refs.formdata.dialogShow = true
    },
    // 打开弹窗
    showTanchaung() {
      this.$refs.formdata.dialogShow = true
    },
    // 删除
    async delHandle(id, name) {
      console.log(id)
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示')
      await delInfo(id)
      const res = await getXuekeInfo1(this.list)
      const resu = res.list.map(item => {
        return item.subject_name
      })
      console.log(name)
      console.log(resu)
      if (resu.includes(name)) {
        return this.$message.error('存在目录关联该学科，故学科不允许删除')
      }
      // 判断页数条数
      if (this.tableData.length === 1 && this.list.page > 1) {
        this.list.page--
      }

      this.$message.success('删除成功')
      this.getInfo()
    },
    // 开关
    async swithChange(id, is_show) {
      console.log(id, is_show)
      const res = { id, is_show }
      await changeSwith(res)
      this.$message.success('更改成功')
    },
    // 展示图片
    showimg(url) {
      return process.env.VUE_APP_BASE_API + '/' + url
    },
    // 获取信息
    async getInfo() {
      const res = await getXuekeInfo(this.list)
      console.log(res)
      this.total = res.total
      this.tableData = res.list
      console.log(this.tableData)
    },
    onSubmit() {
      console.log('submit!')
      this.getInfo()
    },
    // 重置
    reset() {
      this.list = {
        subject_name: '',
        page: 1,
        is_show: '',
        limit: 10
      }
      this.getInfo()
    },
    // 当前页
    handleCurrentChange(val) {
      this.list.page = val
      // 更新列表
      this.getInfo()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.list.limit = val
      // 更新列表
      this.getInfo()
    }
  }
}
</script>

<style>

</style>
